/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameCommanButton.java
 *
 * Created on 2011-05-24, 04:15:16
 */
package jtmcraft.game;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 *
 * @author tom
 */
public class GameButtonMenu extends javax.swing.JPanel {

    private BufferedImage backbuffor;
    private BufferedImage emptybuffor;
    private Graphics2D bbGd2;
    private Graphics2D ebGd2;
    private Color backgroundColor;
    private boolean init = false;

    private List< CommandButton > commandButtons;

    /** Creates new form GameCommanButton */
    public GameButtonMenu() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(0, 0, 0));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        // TODO add your handling code here:

        int xMouse = evt.getX();
        int yMouse = evt.getY();

        if( commandButtons != null )
        {
            for( CommandButton commandButton : commandButtons )
            {
                if( commandButton.getState() != CommandButton.ButtonState.Push )
                {
                    if( commandButton.hitTest( xMouse, yMouse) )
                    {
                        commandButton.setState(CommandButton.ButtonState.MoveOver );
                    } else {
                        commandButton.setState(CommandButton.ButtonState.Normal );
                    }

                }
                
            }
        }

    }//GEN-LAST:event_formMouseMoved

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        int xMouse = evt.getX();
        int yMouse = evt.getY();

        if( commandButtons != null )
        {
            for( CommandButton commandButton : commandButtons )
            {
                if( commandButton.getState() != CommandButton.ButtonState.Push )
                {
                    if( commandButton.hitTest( xMouse, yMouse) )
                    {
                        commandButton.setState(CommandButton.ButtonState.Push );
                        commandButton.fireCommand();
                    } 
                }

            }
        }
    }//GEN-LAST:event_formMousePressed

    public void Init() {
        Init(getParent().getBackground() );
    }

    public void Init(Color backgroundColor) {

        backbuffor = getGraphicsConfiguration().createCompatibleImage(getWidth(), getHeight());
        emptybuffor = getGraphicsConfiguration().createCompatibleImage(getWidth(), getHeight());
        bbGd2 = backbuffor.createGraphics();
        ebGd2 = emptybuffor.createGraphics();

        bbGd2.setColor(backgroundColor);
        bbGd2.fillRect(0, 0, getWidth(), getHeight());
        ebGd2.setColor(backgroundColor);
        ebGd2.fillRect(0, 0, getWidth(), getHeight());
        ebGd2.setColor( Color.GRAY );
        ebGd2.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        init = true;
        clearBackBuffer();
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public void update() {

        if (init) {
            if (commandButtons != null) {
                for (CommandButton commandButton : commandButtons) {
                    commandButton.update();
                }
            }
            Graphics2D gd2 = (Graphics2D) getGraphics();
            gd2.drawImage(backbuffor, 0, 0, null);
        }
    }

    public void clearBackBuffer() {
        if (init) {
            bbGd2.drawImage(emptybuffor, 0, 0, null);
        }
    }

    public void draw()
    {
        if( !init ) return;
        if( commandButtons == null ){
            clearBackBuffer();
            return;
        }

        for( CommandButton commandButton : commandButtons )
        {
            drawCommandButton(commandButton);
        }
    }

    public void drawCommandButton(CommandButton commandButton) {
        int x = commandButton.getX();
        int y = commandButton.getY();
        int width = commandButton.getWidth();
        int height = commandButton.getHeigth();

        bbGd2.drawImage(emptybuffor.getSubimage(x, y, width, height), x, y, null);
        bbGd2.drawImage(commandButton.getImage(), x, y, null);
    }

    public List<CommandButton> getCommandButtons() {
        return commandButtons;
    }

    public void setCommandButtons(List<CommandButton> commandButtons) {
        this.commandButtons = commandButtons;
    }


    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
